# main.py - Entry point for the Football Video Cutter application

import sys
import os
import logging
from PyQt5.QtWidgets import QApplication

from gui.main_window import MainWindow

# Setup logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler("football_cutter.log"),
        logging.StreamHandler(sys.stdout)
    ]
)

logger = logging.getLogger(__name__)


def check_dependencies():
    """Check if all required dependencies are installed"""
    try:
        import cv2
        import torch
        import ultralytics
        import numpy as np
        import ffmpeg
        logger.info("All dependencies successfully loaded")
        return True
    except ImportError as e:
        logger.error(f"Missing dependency: {e}")
        return False


def setup_folders():
    """Create necessary folders if they don't exist"""
    folders = ['output/shots', 'output/stadium', 'output/celebration', 'temp', 'logs']
    for folder in folders:
        os.makedirs(folder, exist_ok=True)
        logger.info(f"Created folder: {folder}")


def main():
    """Main application entry point"""
    logger.info("Starting Football Video Cutter application")

    # Check dependencies
    if not check_dependencies():
        logger.error("Missing dependencies. Please install all requirements from requirements.txt")
        return

    # Create required folders
    setup_folders()

    # Start the GUI application
    app = QApplication(sys.argv)
    app.setApplicationName("Football Video Cutter")
    window = MainWindow()
    window.show()

    sys.exit(app.exec_())


if __name__ == "__main__":
    main()